package com.jb.repair.fragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.jb.repair.R;
import com.jb.repair.adapter.DocAdapter;
import com.jb.repair.adapter.DocMenuH1Adapter;
import com.jb.repair.dao.Db;
import com.jb.repair.model.DocH1Model;
import com.jb.repair.model.DocH2Model;
import com.jb.repair.model.DocModel;
import com.jb.repair.util.CheckFileDialogUtil;
import com.jb.repair.view.IconFontEditText;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by gaobin on 2015/11/17.
 */
public class DocFrament extends Fragment {
    private ListView lv_menu, lv_data;
    private IconFontEditText et_search;
    private List<DocH2Model> docH2List = null;
    private List<DocH1Model> docH1List = new ArrayList<>();
    private List<DocModel> docList = new ArrayList<>();
    private DocModel docModel = null;
    private DocMenuH1Adapter menuH1Adapter;
    private DocAdapter docAdapter, docAdapter1;
    private Db db;
    private CheckFileDialogUtil dialogUtil;

    public DocFrament() {
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.frament_doc, null);
        lv_data = (ListView) v.findViewById(R.id.lv_data);
        lv_data.setTextFilterEnabled(true);
        lv_menu = (ListView) v.findViewById(R.id.lv_menu);
        et_search = (IconFontEditText) v.findViewById(R.id.search_et);
        et_search.addTextChangedListener(textWatcher);

        db = Db.getInstance(getActivity());
        docList = db.getAllDocs();
        docH1List = db.getAllDocsTypeByPid();
        for (int i = 0; i < docH1List.size(); i++) {
            docH2List = new ArrayList<>();
            docH2List = db.getAllDocsTypeByPid2(docH1List.get(i).guid);
            docH1List.get(i).list = docH2List;
        }

        docAdapter = new DocAdapter(getActivity(), docList);
        lv_data.setAdapter(docAdapter);

        menuH1Adapter = new DocMenuH1Adapter(getActivity(), docH1List, new DocMenuH1Adapter.OnItemChildListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String guid = docH2List.get(position).guid;
                docList.clear();
                docList.addAll(db.getAllDocsById(guid));
                docAdapter.notifyDataSetChanged();
            }
        });
        lv_menu.setAdapter(menuH1Adapter);
        lv_menu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                docList.clear();
                docList.addAll(db.getAllDocs());
                docAdapter.notifyDataSetChanged();
                Log.i("iii","1234546788");
            }
        });


        lv_data.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                dialogUtil = new CheckFileDialogUtil(getActivity(), R.style.myDialog);
                docModel = new DocModel();
                docModel = docList.get(position);
                Log.i("iii", "LL" + docModel.toString());
//                dialogUtil.setTvWjmc(docModel.getDocName());
//                dialogUtil.setTvUname(docModel.getCreatPerson());
//                dialogUtil.setTvDepartment(docModel.getDepartment());
//                dialogUtil.setTvTime(docModel.getDate());

                dialogUtil.show();

                return true;
            }
        });

        lv_data.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                AlertDialog dialog = new AlertDialog.Builder(getActivity())
                        //.setTitle("点击事件提示框")
                        .setMessage("操作")
                        .setPositiveButton("下载", null)
                        .setNegativeButton("查看", null)
                        .show();
            }
        });

        return v;
    }


    //模糊查询
    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
//            docAdapter.getFilter().filter(s);//传入数据
//            Toast.makeText(getActivity(), "tttttttt", Toast.LENGTH_SHORT).show();
            String aa = s.toString();
            Pattern p = Pattern.compile(aa);
            //创建一个List集合，List集合的元素是Map
            List<DocModel> listItems = new ArrayList<DocModel>();
            DocModel d = new DocModel();

            for (int i = 0; i < docList.size(); i++) {
                DocModel pmi = docList.get(i);
                String pmiTitle = pmi.getDocName();
                Matcher matcher = p.matcher(pmiTitle);
                if (matcher.find()) {

                    listItems.add(docList.get(i));
                }

            }
            //创建一个SimpleAdapter，设置新的Adapter，替换原来的Adapter
            docAdapter1 = new DocAdapter(getActivity(), listItems
            );
            lv_data.setAdapter(docAdapter1);
            if (TextUtils.isEmpty(s)) {
                lv_data.clearTextFilter();
            } else {
                lv_data.setFilterText(s.toString());
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
            String key = et_search.getText().toString().trim();
            if (s == null) {
                lv_data.setAdapter(docAdapter);
            }
        }
    };


}
